<?php
/**
 * 帐号管理
 */
namespace app\admins\controller\sys;
use \app\admins\controller\Auth;

class Account extends Auth{
    
    
    /**
     * 账户列表
     */
    public function index(){        
        $keyword = input('keyword');
        $roleid  = input('roleid');
        $status  = input('status');
        $where = [
            'page'      => input('page'),
            'limit'     => 20,
            'keyword'   => $keyword,
            'roleid'    => $roleid,
            'status'    => $status
        ];
        $AdminManage = new \logic\admin\AdminManage($this->getUser());
        $data = $AdminManage->getList($where);
        $RoleManage = new \logic\sys\power\Role($this->getUser());
        $rolelist = $RoleManage->getList(['limit'=> 9999])['list'];
        $data['total'] = 100;
        $showpage = showpage($data['total'],$data['page'],$data['limit']);
        $this->assign(get_defined_vars());
        return $this->fetch();
    }
    
    /**
     * 添加账号
     */
    public function add(){
        try{
            if( $_POST ){
                $param = array(
                    'account'   => input('account'),
                    'password'  => input('password'),
                    'confirmpwd'=> input('confirmPwd'),
                    'name'      => input('name'),
                    'mobile'    => input('mobile'),
                    'roleid'    => input('roleid')
                );
                $AdminManage = new \logic\admin\AdminManage($this->getUser());
                $result = $AdminManage->add($param);
                return $this->success('添加成功','/sys/account/index');
            }
            
            $Role = new \logic\sys\power\Role($this->getUser());
            $roleList = $Role->getList(['plat'=>$this->plat , 'limit' => 9999 ,'status' => 1 ])['list'];
            $this->assign(get_defined_vars());
            return $this->fetch();
        }catch(\think\Exception $e){
            $this->error($e->getMessage());
        }
    }
    
    /**
     * 修改账户
     */
    public function edit(){
        try{
            $adminid = input('adminid');
            if(request()->isPost()){
                $param = array(
                    'account'   => input('account'),
                    'password'  => input('password'),
                    'confirmpwd'=> input('confirmPwd'),
                    'name'      => input('name'),
                    'mobile'    => input('mobile'),
                    'roleid'    => input('roleid')
                );
                $AdminManage = new \logic\admin\AdminManage($this->getUser());
                $result = $AdminManage->edit($adminid,$param);
                return $this->success('修改成功','/sys/account/index');
                exit;
            }
            $Role = new \logic\sys\power\Role($this->getUser());
            $roleList = $Role->getList(['plat'=>$this->plat , 'limit' => 9999 ,'status' => 1 ])['list'];
            
            $AdminManage = new \logic\admin\AdminManage($this->getUser());
            $admin = $AdminManage->find(['adminid'=>$adminid]);            
            $this->assign(get_defined_vars());
            return $this->fetch();
        }catch( \think\Exception $e){
            return $this->error( $e->getMessage());
        }
    }
    
    /**
     * 更新账户状态
     */
    public function editStatus(){
        try{
            $id = input('adminid');
            $AdminManage = new \logic\admin\AdminManage($this->getUser());
            $result = $AdminManage->editStatus($id);
            return $this->success('成功');
        }catch(Exception $e){
            return $this->error('失败');
        }
    }
    
    
}